package core.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

/**
 * 分页类
 * @author qwx
 *
 */
public class Pager {
  
  private int elementsCount;   // 总的元素数
  private int pageSize;        // 页面大小, 每页显示多少条数
  private int pagesCount;      // 总的页面数
  private int currentPage =1;  // 当前是第几页
  private int priCurrNo = 5;   // 默认当前页前面的页数为5页
  
  public int getPriCurrNo() {
    return priCurrNo;
  }

  public void setPriCurrNo(int priCurrNo) {
    this.priCurrNo = priCurrNo;
  }

  private List<?> objs= new ArrayList();  //查询出的类
  
  public int getElementsCount() {
    return elementsCount;
  }
  
  public void setElementsCount(int elementsCount) {
    this.elementsCount = elementsCount;
  }
  
  public int getPageSize() {
    return pageSize;
  }
  
  public void setPageSize(int pageSize) {
    this.pageSize = pageSize;
  }

  public void setPagesCount(int pagesCount) {
    this.pagesCount = pagesCount;
  }
  
  public int getCurrentPage() {
    return currentPage;
  }
  
  public List<?> getObjs() {
    return objs;
  }
  
  public void setObjs(List<?> objs) {
    this.objs = objs;
  }
  
  public void setCurrentPage(int currentPage) {
    this.currentPage = currentPage;
  }
  
  public int getPagesCount() {
    pagesCount = (getElementsCount() % getPageSize() == 0) ? (getElementsCount() / getPageSize())
        : (getElementsCount() / getPageSize() + 1);
    return pagesCount;
  }
  
  /**
   * 生成分页的号码
   * @return
   * @throws Exception 
   */
  public String toPagerJs( int state ) throws Exception{  // js拼成页码
    String pageNos = "";
    //当前页之前的页码
    int currNo = this.getCurrentPage();
    int priNo = this.getPriCurrNo();
    int total = this.getPagesCount();
    if(currNo > total){
      currNo = total;
    }
    // currNo前面的部分
    if(currNo - priNo>0){
      for(int i=currNo - priNo; i<currNo; i++){
        pageNos += i + ",";
      }
    }else{
      for(int j=1; j<currNo; j++){
        pageNos += j + ",";
      }
    }
    pageNos += "" + currNo + ",";   //当前页
    
    if(currNo + priNo < total){
      for(int k = currNo+1; k <= currNo+priNo; k++){
        pageNos += k + ",";
      }
    }else{
      for(int m=currNo+1; m <= total; m++){
        pageNos += m + ",";
      }
    }
    if( !StringUtils.isEmpty(pageNos) ){
      int k = pageNos.length();
      int index = pageNos.lastIndexOf(",");
      if(k == index+1){
        pageNos = pageNos.substring(0, index);
      }
    }else{
      pageNos = "";
    }
    JSONObject obj = new JSONObject();
    obj.put(Const.STATE, 0);
    obj.put("currNo", getCurrentPage());
    obj.put("elmcnt", getElementsCount());
    obj.put("totalPage", getPagesCount());
    obj.put("nos", pageNos);
    obj.put("pageSize", pageSize);
    if ( state == 0 ) {
      obj.put("data", toPageAndDatas());
    } else {
      obj.put("data", toPageAndData());
    }
    return obj.toString();
  }
  
  @SuppressWarnings("unchecked")
  private String toPageAndDatas() throws Exception{  //js拼成页码和数据
    StringBuffer sb = new StringBuffer();
    if(objs == null || objs.size() ==0){
      return "[]";
    }
    sb.append("[");
    for(int i=0; i<objs.size(); i++){
      sb.append(map2Json((Map)objs.get(i)));
      if(i < objs.size() -1){
        sb.append(",");
      }
    }
    sb.append("]");
    return sb.toString();
  }
  
  @SuppressWarnings("unchecked")
  private String toPageAndData() throws Exception{  //js拼成页码和数据
    JSONArray array = new JSONArray();
    if(objs == null || objs.size() ==0){
      return array.toString();
    }
    for ( int i=0; i<objs.size(); i++ ) {
      array.add( objs.get(i) );
    }
    return array.toString();
  }
  
  public  String map2Json(Map<String, Object> map) {
    if ( map != null && map.size() >0) {
      JSONObject obj = new JSONObject();
      for (Map.Entry<String, Object> kv : map.entrySet()) {
       obj.put(kv.getKey(), kv.getValue()); 
      }
      return obj.toString();
    }
    return "";
  }
}
